package com.burgeon.backend.SHOPBACKEND.push;

import com.burgeon.backend.SHOPBACKEND.push.android.*;
import com.burgeon.backend.SHOPBACKEND.push.ios.*;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.List;

public class UmengUtil {

    private String appkey = "5f2bd93dd3093221547598a5";
    private String appMasterSecret = "edxx4ryqipyjut4aa9ksqmyh8jviw3u5";
    private String timestamp = null;
    private PushClient client = new PushClient();

    public void sendAndroidBroadcast() throws Exception {
        AndroidBroadcast broadcast = new AndroidBroadcast(appkey,appMasterSecret);
        broadcast.setTicker( "Android broadcast ticker");
        broadcast.setTitle(  "中文的title");
        broadcast.setText(   "Android broadcast text");
        broadcast.goAppAfterOpen();
        broadcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        // TODO Set 'production_mode' to 'false' if it's a test device.
        // For how to register a test device, please see the developer doc.
        broadcast.setProductionMode();
        // Set customized fields
        broadcast.setExtraField("test", "helloworld");
        //厂商通道相关参数
        broadcast.setChannelActivity("your channel activity");
        broadcast.setChannelProperties("abc");
        client.send(broadcast);
    }

    /**
     * unicast-单播
     * @throws Exception
     */
    public void sendAndroidUnicast(String deviceToken, String ticker,String title,String test) throws Exception {
        AndroidUnicast unicast = new AndroidUnicast(appkey,appMasterSecret);
        // TODO Set your device token
        // 当display_type=message时，body的内容只需填写custom字段。
        // 当display_type=notification时，body包含如下参数:
        // 通知展现内容:
//		"ticker":"xx",    // 必填，通知栏提示文字
//				"title":"xx",    // 必填，通知标题
//				"text":"xx",    // 必填，通知文字描述
//
        unicast.setDeviceToken(deviceToken);
        unicast.setTicker(ticker);
        unicast.setTitle(title);
        unicast.setText(test);
        unicast.goAppAfterOpen();/////点击"通知"的后续行为，默认为打开app。
//		"display_type":"xx",    // 必填，消息类型: notification(通知)、message(消息)
        ///通知:消息送达到用户设备后，由友盟SDK接管处理并在通知栏上显示通知内容。
        unicast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        // TODO Set 'production_mode' to 'false' if it's a test device.
        // For how to register a test device, please see the developer doc.
//		"production_mode":"true/false",    // 可选，正式/测试模式。默认为true
        unicast.setProductionMode();
        // Set customized fields
//		extra:{    // 可选，JSON格式，用户自定义key-value。只对"通知"
//			// (display_type=notification)生效。
//			// 可以配合通知到达后，打开App/URL/Activity使用。
//			"key1": "value1",
//			"key2": "value2",
//        unicast.setExtraField("test", "helloworld");

//		"mipush": "true/false",    // 可选，默认为false。当为true时，表示MIUI、EMUI、Flyme系统设备离线转为系统下发
//				"mi_activity": "xx",    // 可选，mipush值为true时生效，表示走系统通道时打开指定页面acitivity的完整包路径。
//		unicast.setChannelActivity("your channel activity");
        //		"channel_properties":{ //可选，厂商通道相关的特殊配置
//		unicast.setChannelProperties("abc");
        System.err.println(unicast.rootJson);
        client.send(unicast);
    }

    public void sendAndroidGroupcast() throws Exception {
        AndroidGroupcast groupcast = new AndroidGroupcast(appkey,appMasterSecret);
        /*  TODO
         *  Construct the filter condition:
         *  "where":
         *	{
         *		"and":
         *		[
         *			{"tag":"test"},
         *			{"tag":"Test"}
         *		]
         *	}
         */
        JSONObject filterJson = new JSONObject();
        JSONObject whereJson = new JSONObject();
        JSONArray tagArray = new JSONArray();
        JSONObject testTag = new JSONObject();
        JSONObject TestTag = new JSONObject();
        testTag.put("tag", "test");
        TestTag.put("tag", "Test");
        tagArray.put(testTag);
        tagArray.put(TestTag);
        whereJson.put("and", tagArray);
        filterJson.put("where", whereJson);

        groupcast.setFilter(filterJson);
        groupcast.setTicker( "Android groupcast ticker");
        groupcast.setTitle(  "中文的title");
        groupcast.setText(   "Android groupcast text");
        groupcast.goAppAfterOpen();
        groupcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        groupcast.setChannelActivity("your channel activity");
        // TODO Set 'production_mode' to 'false' if it's a test device.
        // For how to register a test device, please see the developer doc.
        groupcast.setProductionMode();
        //厂商通道相关参数
        groupcast.setChannelActivity("your channel activity");
        groupcast.setChannelProperties("abc");
        client.send(groupcast);
    }

    //发送给就提客户
    public Boolean sendAndroidCustomizedcast(String alias, String aliasType, String ticker,
                                          String title, String text, String id,String channel,String extraType) throws Exception {
        AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey,appMasterSecret);
        // TODO Set your alias here, and use comma to split them if there are multiple alias.
        // And if you have many alias, you can also upload a file containing these alias, then
        // use file_id to send customized notification.
       // System.err.println(alias.toString());
        //System.err.println("友盟方法sendAndroidCustomizedcast："+alias);
        customizedcast.setAlias(alias, aliasType);
        customizedcast.setTicker(ticker);
        customizedcast.setTitle(title);
        customizedcast.setText(text);
        //System.err.println("youmeng"+text);
        customizedcast.goCustomAfterOpen(id);
        customizedcast.setExtraField("type", extraType);//1是消息2是订单
        customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        // TODO Set 'production_mode' to 'false' if it's a test device.
        // For how to register a test device, please see the developer doc.
        customizedcast.setProductionMode();
        //厂商通道相关参数
		customizedcast.setChannelActivity(channel);//activity的全包路径
		//customizedcast.setChannelProperties("abc");
      //  System.err.println(customizedcast.getClass().toString().replaceAll("'\'","'"));
        return client.send(customizedcast);
    }
    //发送给就提客户
    public void sendAndroidAliasList(List<String> alias, String aliasType, String ticker,
                                          String title, String text, String id,String channel) throws Exception {
        AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey,appMasterSecret);
        // TODO Set your alias here, and use comma to split them if there are multiple alias.
        // And if you have many alias, you can also upload a file containing these alias, then
        // use file_id to send customized notification.
        System.err.println(alias.toString());
        customizedcast.setAlias(alias.toString(), aliasType);
        customizedcast.setTicker(ticker);
        customizedcast.setTitle(title);
        customizedcast.setText(text);
        customizedcast.goCustomAfterOpen(id);
        customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        // TODO Set 'production_mode' to 'false' if it's a test device.
        // For how to register a test device, please see the developer doc.
        customizedcast.setProductionMode();
        //厂商通道相关参数
        customizedcast.setChannelActivity(channel);//activity的全包路径
        //customizedcast.setChannelProperties("abc");
        System.err.println(customizedcast.toString());
        System.err.println("返回信息"+client.send(customizedcast));
    }

    public void sendAndroidCustomizedcastFile() throws Exception {
        AndroidCustomizedcast customizedcast = new AndroidCustomizedcast(appkey,appMasterSecret);
        // TODO Set your alias here, and use comma to split them if there are multiple alias.
        // And if you have many alias, you can also upload a file containing these alias, then
        // use file_id to send customized notification.
        String fileId = client.uploadContents(appkey,appMasterSecret,"aa"+"\n"+"bb"+"\n"+"alias");
        customizedcast.setFileId(fileId, "alias_type");
        customizedcast.setTicker( "Android customizedcast ticker");
        customizedcast.setTitle(  "中文的title");
        customizedcast.setText(   "Android customizedcast text");
        customizedcast.goAppAfterOpen();
        customizedcast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        // TODO Set 'production_mode' to 'false' if it's a test device.
        // For how to register a test device, please see the developer doc.
        customizedcast.setProductionMode();
        //厂商通道相关参数
        customizedcast.setChannelActivity("your channel activity");
        customizedcast.setChannelProperties("abc");
        client.send(customizedcast);
    }

    public void sendAndroidFilecast() throws Exception {
        AndroidFilecast filecast = new AndroidFilecast(appkey,appMasterSecret);
        // TODO upload your device tokens, and use '\n' to split them if there are multiple tokens
        String fileId = client.uploadContents(appkey,appMasterSecret,"aa"+"\n"+"bb");
        filecast.setFileId( fileId);
        filecast.setTicker( "Android filecast ticker");
        filecast.setTitle(  "中文的title");
        filecast.setText(   "Android filecast text");
        filecast.goAppAfterOpen();
        filecast.setDisplayType(AndroidNotification.DisplayType.NOTIFICATION);
        //厂商通道相关参数
        filecast.setChannelActivity("your channel activity");
        filecast.setChannelProperties("abc");
        client.send(filecast);
    }

    public void sendIOSBroadcast() throws Exception {
        IOSBroadcast broadcast = new IOSBroadcast(appkey,appMasterSecret);
        //alert值设置为字符串
        //broadcast.setAlert("IOS 广播测试");
        //alert的值设置为字典
        broadcast.setAlert("今日天气" , "" , "今日可能下雨🌂");
        broadcast.setBadge( 0);
        broadcast.setSound( "default");
        // TODO set 'production_mode' to 'true' if your app is under production mode
        broadcast.setTestMode();
        // Set customized fields
        broadcast.setCustomizedField("test", "helloworld");
        client.send(broadcast);
    }

    public void sendIOSUnicast() throws Exception {
        IOSUnicast unicast = new IOSUnicast(appkey,appMasterSecret);
        // TODO Set your device token
        unicast.setDeviceToken( "your device_token");
        //alert值设置为字符串
        //unicast.setAlert("IOS 单播测试");
        //alert的值设置为字典
        unicast.setAlert("今日天气" , "" , "今日可能下雨🌂");
        unicast.setBadge( 0);
        unicast.setSound( "default");
        // TODO set 'production_mode' to 'true' if your app is under production mode
        unicast.setTestMode();
        // Set customized fields
        unicast.setCustomizedField("test", "helloworld");
        client.send(unicast);
    }


    public void sendIOSGroupcast() throws Exception {
        IOSGroupcast groupcast = new IOSGroupcast(appkey,appMasterSecret);
        /*  TODO
         *  Construct the filter condition:
         *  "where":
         *	{
         *		"and":
         *		[
         *			{"tag":"iostest"}
         *		]
         *	}
         */
        JSONObject filterJson = new JSONObject();
        JSONObject whereJson = new JSONObject();
        JSONArray tagArray = new JSONArray();
        JSONObject testTag = new JSONObject();
        testTag.put("tag", "iostest");
        tagArray.put(testTag);
        whereJson.put("and", tagArray);
        filterJson.put("where", whereJson);


        // Set filter condition into rootJson
        groupcast.setFilter(filterJson);
        //groupcast.setAlert("IOS 组播测试");
        //alert的值设置为字典
        groupcast.setAlert("今日天气" , "subtitle" , "今日可能下雨🌂");
        groupcast.setBadge( 0);
        groupcast.setSound( "default");
        // TODO set 'production_mode' to 'true' if your app is under production mode
        groupcast.setTestMode();
        client.send(groupcast);
    }

    public void sendIOSCustomizedcast() throws Exception {
        IOSCustomizedcast customizedcast = new IOSCustomizedcast(appkey,appMasterSecret);
        // TODO Set your alias and alias_type here, and use comma to split them if there are multiple alias.
        // And if you have many alias, you can also upload a file containing these alias, then
        // use file_id to send customized notification.
        customizedcast.setAlias("alias", "alias_type");
        //customizedcast.setAlert("IOS 个性化测试");
        //alert的值设置为字典
        customizedcast.setAlert("今日天气" , "" , "今日可能下雨🌂");
        customizedcast.setBadge( 0);
        customizedcast.setSound( "default");
        // TODO set 'production_mode' to 'true' if your app is under production mode
        customizedcast.setTestMode();
        client.send(customizedcast);
    }

    public void sendIOSFilecast() throws Exception {
        IOSFilecast filecast = new IOSFilecast(appkey,appMasterSecret);
        // TODO upload your device tokens, and use '\n' to split them if there are multiple tokens
        String fileId = client.uploadContents(appkey,appMasterSecret,"aa"+"\n"+"bb");
        filecast.setFileId( fileId);
        //filecast.setAlert("IOS 文件播测试");
        //alert的值设置为字典
        filecast.setAlert("今日天气" , "" , "今日可能下雨🌂");
        filecast.setBadge( 0);
        filecast.setSound( "default");
        // TODO set 'production_mode' to 'true' if your app is under production mode
        filecast.setTestMode();
        client.send(filecast);
    }
}
